<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>OleanderAPP</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            margin: 0;
            padding: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        .container {
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 350px;
            padding: 20px;
            text-align: center;
        }
        h1 {
            color: #333;
            margin-bottom: 20px;
        }
        input[type="file"] {
            margin-bottom: 10px;
        }
        button {
            margin: 5px;
            padding: 10px 15px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        select {
            width: calc(100% - 22px);
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        pre {
            background-color: #f9f9f9;
            border: 1px solid #ddd;
            padding: 10px;
            border-radius: 5px;
            overflow-y: auto;
            max-height: 150px;
        }
    </style>
    <script>
        window.onload = function() {
            const urlParams = new URLSearchParams(window.location.search);
            let appid = urlParams.get('appid');
            if (appid) {
                document.open();
                document.write(localStorage.getItem(appid));
                document.close();
            } else {
                populateDropdown();
            }
        }
    </script>
</head>
<body>
    <div class="container">
        <h1>选择一个 .oap 安装包并安装app</h1>
        <input type="file" id="fileInput" accept=".oap" />
        <button onclick="readFile()">安装</button>
        <pre id="fileContent"></pre>
        <select id="appSelect">
        </select>
        <button onclick="deleteSelectedOption()">卸载此App</button>
        <button onclick="manageSelectedOption()">管理此App</button>
        <button onclick="openSelectedOption()">启动此App</button>
        <br>
        <select id="permissionSelect">
        </select>
        <button onclick="disablePermission()">禁用此权限</button>
    </div>
    <script>
        function readFile() {
            const fileInput = document.getElementById('fileInput');
            const file = fileInput.files[0];
            const reader = new FileReader();
            reader.onload = function(event) {
                const fileContent = event.target.result;
                appid = Date.now().toString()
                localStorage.setItem(appid, fileContent);
                let currentUrl = window.location.href;
                if (currentUrl.indexOf('?') === -1) {
                    currentUrl += `?appid=${appid}`;
                } else {
                    currentUrl += `&appid=${appid}`;
                }
                function extractContent(inputString, regex) {
                    const match = inputString.match(regex);
                    if (match && match[1]) {
                        return match[1].trim();
                    }
                    return null;
                }
                let OleanderAPP_json_w;
                try {
                    OleanderAPP_json_w = JSON.parse(localStorage.getItem("OleanderAPP_json")) || [];
                } catch (error) {
                    OleanderAPP_json_w = [];
                }
                OleanderAPP_json_w.push({
                    "appid":appid,
                    "name":extractContent(fileContent, "/<title>(.*?)<\/title>/"),
                    "projectName":extractContent(fileContent, "/<!-- Project: (.*?) -->/")
                });
                localStorage.setItem("OleanderAPP_json", JSON.stringify(OleanderAPP_json_w));
                window.location.assign(currentUrl);
            };
            reader.onerror = function() {
                alert('文件读取出错，请重试。');
            };
            reader.readAsText(file);
        }
        function populateDropdown() {
            const selectElement = document.getElementById('appSelect');
            let OleanderAPP_json_r;
            try {
                OleanderAPP_json_r = JSON.parse(localStorage.getItem("OleanderAPP_json")) || [];
            } catch (error) {
                OleanderAPP_json_r = [];
            }
            OleanderAPP_json_r.forEach(oleanderapp => {
                let appName = oleanderapp.name;
                const option = document.createElement('option');
                option.value = appName;
                option.textContent = appName;
                selectElement.appendChild(option);
            });
        }
        function deleteSelectedOption() {
            let OleanderAPP_json_d;
                try {
                    OleanderAPP_json_d = JSON.parse(localStorage.getItem("OleanderAPP_json")) || [];
                } catch (error) {
                    OleanderAPP_json_d = [];
                }
            const selectElement = document.getElementById('appSelect');
            const selectedIndex = selectElement.selectedIndex;
            if (selectedIndex !== -1) {
                const selectedValue = selectElement.options[selectedIndex].value;
                selectElement.remove(selectedIndex);
                for (let i = 0; i < OleanderAPP_json_d.length; i++) {
                    i_c = OleanderAPP_json_d[i];
                    if (i_c.name === selectedValue) {
                        localStorage.removeItem(i_c.appid);
                    }
                }
                OleanderAPP_json_d = OleanderAPP_json_d.filter(item => item.name !== selectedValue);
                localStorage.setItem("OleanderAPP_json", OleanderAPP_json_d);
            } else {
                alert("请选择要删除的APP");
            }
        }
        function manageSelectedOption() {
            let OleanderAPP_json_m;
                try {
                    OleanderAPP_json_m = JSON.parse(localStorage.getItem("OleanderAPP_json")) || [];
                } catch (error) {
                    OleanderAPP_json_m = [];
                }
            const selectElement = document.getElementById('permissionSelect');
            const APPselectElement = document.getElementById('appSelect');
            const APPselectedIndex = APPselectElement.selectedIndex;
            if (APPselectedIndex !== -1) {
                const APPselectedValue = APPselectElement.options[APPselectedIndex].value;
                for (let i = 0; i < OleanderAPP_json_m.length; i++) {
                    i_c = OleanderAPP_json_m[i];
                    if (i_c.name === APPselectedValue) {
                        let rights_name_json = JSON.parse(localStorage.getItem(i_c.projectName+'/rights')) || [];
                        rights_name_json.forEach(rights_name => {
                            const option = document.createElement('option');
                            option.value = rights_name;
                            option.textContent = rights_name;
                            selectElement.appendChild(option);
                        });
                    }
                }
            } else {
                alert("请选择要管理的APP");
            }
        }
        function disablePermission() {
            let OleanderAPP_json_dp;
            try {
                OleanderAPP_json_dp = JSON.parse(localStorage.getItem("OleanderAPP_json")) || [];
            } catch (error) {
                OleanderAPP_json_dp = [];
            }
            const selectElement = document.getElementById('permissionSelect');
            const selectedIndex = selectElement.selectedIndex;
            if (selectedIndex !== -1) {
                const selectedValue = selectElement.options[selectedIndex].value;
                const APPselectElement = document.getElementById('appSelect');
                const APPselectedIndex = selectElement.selectedIndex;
                const APPselectedValue = APPselectElement.options[APPselectedIndex].value;
                let rights_name_json = []
                let projectName = ""
                for (let i = 0; i < OleanderAPP_json_dp.length; i++) {
                    i_c = OleanderAPP_json_dp[i];
                    if (i_c.name === APPselectedValue) {
                        projectName = i_c.projectName
                        rights_name_json = JSON.parse(localStorage.getItem(projectName+'/rights')) || [];
                    }
                }
                rights_name_json = rights_name_json.filter(item => item == selectedValue);
                if (rights_name_json == "") {
                    localStorage.removeItem(projectName+'/rights');
                } else {
                    localStorage.setItem(projectName+'/rights', rights_name_json);
                }
                selectElement.remove(selectedIndex);
            } else {
                alert("请选择要禁用的权限");
            }
        }
        function openSelectedOption() {
            let OleanderAPP_json_o;
            try {
                OleanderAPP_json_o = JSON.parse(localStorage.getItem("OleanderAPP_json")) || [];
            } catch (error) {
                OleanderAPP_json_o = [];
            }
            const selectElement = document.getElementById('appSelect');
            const selectedIndex = selectElement.selectedIndex;
            if (selectedIndex !== -1) {
                const selectedValue = selectElement.options[selectedIndex].value;
                selectElement.remove(selectedIndex);
                for (let i = 0; i < OleanderAPP_json_o.length; i++) {
                    i_c = OleanderAPP_json_o[i];
                    if (i_c.name === selectedValue) {
                        let currentUrl = window.location.href;
                        if (currentUrl.indexOf('?') === -1) {
                            currentUrl += `?appid=${i_c.appid}`;
                        } else {
                            currentUrl += `&appid=${i_c.appid}`;
                        }
                        window.location.assign(currentUrl);
                    }
                }
            } else {
                alert("请选择要启动的APP");
            }
        }
    </script>
</body>
</html>